// pages/hospital/hospital.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    chooseLoaction: 0,
    location: '',
    longitude: 123.287682,
    latitude: 44.124004,
    is_special: [
      {
        value: 0,
        name: '否',
        selected: false
      },
      {
        value: 1,
        name: '是',
        selected: false
      },
    ],
    typeArr: [
      { value: 0, name: '请选择' },
      { value: 1, name: '集体企业' },
      { value: 2, name: '外资企业' },
      { value: 3, name: '私营企业' },
      { value: 4, name: '行政事业单位' },
      { value: 5, name: '个体户' },
      { value: 6, name: '其他企业' },
    ],
    typeIdx: 0,
    flag: 0,
    flag1: 0,
    rawData: [],//后台获取的原始数组
    multiObjArray: [], //整理后在页面循环的数组
    multiIndex: [0, 0],
    comArr: [],
    gridArr: [],
    newArr: [],
    g_id: '',
    c_id: '',
    tempImg: '',
  },

  onLoad(opts) {

  },

  onShow() {
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];   //上一页
    prevPage.setData({
      status: 1
    })
  },

  radioChange(e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    let item1 = this.data.is_special
    for (let i = 0, len = item1.length; i < len; ++i) {
      item1[i].checked = item1[i].value === e.detail.value
    }
    this.setData({
      item1
    })
  },

  typeChange(e) {
    console.log(e)
    this.setData({
      typeIdx: e.detail.value,
      flag1: 1
    })
    console.log(this.data.typeArr[this.data.typeIdx].value)
  },

  formSubmit(e) {
    console.log(e)
    let access_token = wx.getStorageSync('access_token')
    let { c_name, is_special, worker_num, cover_area, legal_person, legal_person_phone, party_clerk, party_clerk_phone, labour_union_master, labour_union_master_phone, labour_vip_num, party_num, safe_master, safe_master_phone, culture_remark, address, remark } = e.detail.value
    let type = this.data.typeArr[this.data.typeIdx].value
    if (e.detail.value.c_name == '') {
      wx.showToast({
        title: '单位名称不能为空！',
        icon: 'none'
      })
      return false
    }
    if (type == '') {
      wx.showToast({
        title: '请选择单位类型！',
        icon: 'none'
      })
      return false
    }
    if (worker_num == '') {
      wx.showToast({
        title: '员工人数不能为空！',
        icon: 'none'
      })
      return false
    }
    if (legal_person == '') {
      wx.showToast({
        title: '法人代表不能为空！',
        icon: 'none'
      })
      return false
    }
    if (legal_person_phone == '') {
      wx.showToast({
        title: '法人联系电话不能为空！',
        icon: 'none'
      })
      return false
    }
    wx.showLoading({
      title: '正在提交...',
      mask: true
    })
    wx.request({
      url: 'https://baokang.wshoto.com/wx/companyadd',
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        access_token: access_token,
        type: type,
        c_name: c_name,
        is_special: is_special,
        worker_num: worker_num,
        cover_area: cover_area,
        legal_person: legal_person,
        legal_person_phone: legal_person_phone,
        party_clerk: party_clerk,
        party_clerk_phone: party_clerk_phone,
        labour_union_master: labour_union_master,
        labour_union_master_phone: labour_union_master_phone,
        labour_vip_num: labour_vip_num,
        party_num: party_num,
        safe_master: safe_master,
        safe_master_phone: safe_master_phone,
        culture_remark: culture_remark,
        address: address,
        remark: remark,
      },
      success: (res) => {
        console.log(res)
        wx.hideLoading()
        if (res.data.code == 1) {
          wx.showModal({
            content: res.data.msg,
            showCancel: false,
            success: (res) => {
              if (res.confirm) {
                wx.navigateBack()
              }
            }
          })
        } else {
          wx.showModal({
            content: res.data.msg,
            showCancel: false,
          })
        }
      }
    })
  }
})